package pro.cherish.smartfile.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.web.reactive.config.CorsRegistry;
import org.springframework.web.reactive.config.ResourceHandlerRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;

/**
 * @Description 类描述信息
 * @ClassName WebConfiguration
 * @Date 2020-4-10 17:07
 * @Version 1.0
 */
@Configuration
@Slf4j
public class WebFluxConfig implements WebFluxConfigurer {


    @Autowired
    private ObjectMapper objectMapper;

    //配置跨域
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "OPTION", "DELETE").maxAge(3600);
    }

    //重新映射路径
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui.html**")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/doc.html**")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/static/**")
                //指向的是目录
                .addResourceLocations("classpath:/static/");
    }

    @Override
    public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
//		configurer.addCustomResolver(new AuthUserResolver());
    }

    /**
     * webFlux返回JSON日期进行格式化，如果不想用全局的配置，可以通过 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 进行特定处理，如果前端传的是日期格式化字符串，则需要考虑时区问题，请使用东八区时区（例如GMT+8）
     *
     * @param configurer
     * @Return void
     * @Author lys
     * @Date 2020-5-29 13:33
     */
    @Override
    public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
        configurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(objectMapper));
        configurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(objectMapper));
        configurer.defaultCodecs().maxInMemorySize(10 * 1000 * 1024);
    }
//
//    @Bean
//    public MultipartConfigElement configFactory() {
//        MultipartConfigFactory factory = new MultipartConfigFactory();
//        factory.setMaxFileSize(DataSize.ofBytes(1024 * 1000));//设置单个文件上传的最大字节数
//        return factory.createMultipartConfig();
//    }
}
